package cn.edu.ytu.edroid.easychart;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.webkit.WebView;

public class ChartActivity extends Activity {
	private String url = "";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.chart_activity);

		DisplayMetrics dm = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(dm);
		int chsWidth = dm.widthPixels;
		int chsHeight = dm.heightPixels;

		WebView wv = (WebView) findViewById(R.id.chart_view);
		url = this.getIntent().getStringExtra("url");
		url += "&chs=" + Integer.toString(chsWidth) + "x"
				+ Integer.toString(chsHeight);
		wv.loadUrl(url);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.chart_menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.send:
			Intent mailIntent = new Intent(Intent.ACTION_SEND);
			mailIntent.putExtra(Intent.EXTRA_SUBJECT, "Here is the chart");
			mailIntent.putExtra(Intent.EXTRA_TEXT, url);
			mailIntent.setType("text/plain");
			startActivity(mailIntent);
			break;
		case R.id.about:
			new AlertDialog.Builder(ChartActivity.this).setTitle("About")
					.setMessage(R.string.about).setPositiveButton("Ok",
							new DialogInterface.OnClickListener() {

								@Override
								public void onClick(DialogInterface dialog,
										int which) {
									// TODO Auto-generated method stub
								}
							}).create().show();
			break;
		case R.id.exit:
			finish();
			break;
		}
		return false;
	}
}
